package com.my.study.globalfilter.publish;

import com.my.study.aop.annotation.FilterType;
import com.my.study.enums.ApiTypeEnum;
import com.my.study.enums.BusinessExceptionEnum;
import com.my.study.handler.WhiteListHandler;
import com.my.study.util.HttpGlobalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@Slf4j
public class PublishFilter implements GlobalFilter, Ordered {

    @Autowired
    private WhiteListHandler whiteListHandler;

    @FilterType(type = ApiTypeEnum.PUBLIC_API)
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取路径
        String path = exchange.getRequest().getURI().getPath();
        if(!whiteListHandler.isWhiteList(path)){
            return HttpGlobalUtil.fail(exchange.getResponse(), BusinessExceptionEnum.WHITE_LIST.code);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 2;
    }
}
